import configparser
import os

from mongo.MongoHelper import MongoHelper
import logging
from tools.log.logU import logU
from tools.conf.confU import confU
from tools.mail.mail import emailUtils

conf = confU().config
log = logU('').logger


def intercept_method(func):
    def wrapper(*args, **kwargs):
        log.info(f"Before: {func.__name__} is called.")
        result = func(*args, **kwargs)
        log.info(f"After: {func.__name__} is called.")
        return result

    return wrapper


class MyService:
    def __init__(self, cfg):
        self.mongoHelper = MongoHelper(url=conf.get('db', 'url'), database=conf.get('db', 'database'))
        log.info("mongodb initialized")

    @intercept_method
    def get_corr_data(self, code='', beg_date=None, end_date=None, market=None):
        basic_coll = self.mongoHelper.mongo_db.get_collection('equityBasic')
        cash_coll = self.mongoHelper.mongo_db.get_collection('individualCashFlow')
        join_key = 'code'
        results = []
        for doc1 in basic_coll.find({'code': code}):
            logU.logger.info(doc1)
            doc2 = cash_coll.find_one({join_key: doc1[join_key]})
            if doc2:
                # 如果找到匹配的文档，将它们合并
                results.append({**doc1.copy(), **doc2.copy()})
        # 输出结果
        for result in results:
            log.info(result)
